
*#include "inkey.ch"
*#include "box.ch"
*#include "set.ch"

Private nValor, cVarExt, nQtdLin, nTamLin, cCharComp, nTamWord
nTamLin := 45
*cVarExt := {}

cVarExt := array(15)

Clear

nValor := 0

@ 00,00 Say "Valor :" Get nValor Picture "@e 999,999,999.99"
Read

aExtenso := {}

Extenso( nValor, aExtenso, 2, 45, "*" )

For t := 1 To Len( aExtenso )

  ?  aExtenso[t]

Next t




Function Extenso( nValor, cVarExt, nQtdLin, nTamLin, cCharComp)



If nValor = 0

   Return

EndIf

If PCount() < 5

   cCharComp := "*"

EndIf

nTamWord := 0
cModo    := Space( 01 )
cPrepos  := Space( 01 )

nUnidade := {}
nDezena  := {}
nCentena := {}
nDecimal := {}

aAdd( nUnidade , "UM"              )
aAdd( nUnidade , "DOIS"            )
aAdd( nUnidade , "TRES"            )
aAdd( nUnidade , "QUA-TRO"         )
aAdd( nUnidade , "CIN-CO"          )
aAdd( nUnidade , "SEIS"            )
aAdd( nUnidade , "SE-TE"           )
aAdd( nUnidade , "OI-TO"           )
aAdd( nUnidade , "NO-VE"           )
aAdd( nUnidade , "DEZ"             )
aAdd( nUnidade , "ON-ZE"           )
aAdd( nUnidade , "DO-ZE"           )
aAdd( nUnidade , "TRE-ZE"          )
aAdd( nUnidade , "QUA-TOR-ZE"      )
aAdd( nUnidade , "QUIN-ZE"         )
aAdd( nUnidade , "DE-ZES-SEIS"     )
aAdd( nUnidade , "DE-ZES-SE-TE"    )
aAdd( nUnidade , "DE-ZOI-TO"       )
aAdd( nUnidade , "DE-ZE-NO-VE"     )

aAdd( nDezena  , ""                )
aAdd( nDezena  , "VIN-TE"          )
aAdd( nDezena  , "TRIN-TA"         )
aAdd( nDezena  , "QUA-REN-TA"      )
aAdd( nDezena  , "CIN-QUEN-TA"     )
aAdd( nDezena  , "SES-SEN-TA"      )
aAdd( nDezena  , "SE-TEN-TA"       )
aAdd( nDezena  , "OI-TEN-TA"       )
aAdd( nDezena  , "NO-VEN-TA"       )

aAdd( nCentena , "CEN-TO"          )
aAdd( nCentena , "DU-ZEN-TOS"      )
aAdd( nCentena , "TRE-ZEN-TOS"     )
aAdd( nCentena , "QUA-TRO-CEN-TOS" )
aAdd( nCentena , "QUI-NHEN-TOS"    )
aAdd( nCentena , "SEIS-CEN-TOS"    )
aAdd( nCentena , "SE-TE-CEN-TOS"   )
aAdd( nCentena , "OI-TO-CEN-TOS"   )
aAdd( nCentena , "NO-VE-CEN-TOS"   )

For a := 1 To 15

   If a <= nQtdLin

      aAdd( cVarExt , "" )

   EndIf

   aAdd( nDecimal  , Val( SubStr( Str( nValor , 15 , 2 ) , a , 1 ) ) )

Next

nIndice := 1
nNumLin := 1

While nIndice < 13

   If Str( nIndice , 2 ) $(" 1 4 710") .And. nDecimal[nIndice] > 0

      If nDecimal[nIndice] = 1 .AND. nDecimal[nIndice+1] = 0 .AND. nDecimal [nIndice+2] = 0

         cWord2 := "CEM"
         cWord1 := "CEM"

      Else

         cWord2 := nCentena[nDecimal[nIndice]]
         cWord1 := StrTran( cWord2 , "-" )

      EndIf

      Escreve( cWord2, cWord1 )

   EndIf

   If Str( nIndice , 2 ) $(" 2 5 811") .And. nDecimal[nIndice] > 0

      If cModo = "P"

         cWord2 := "E"
         cWord1 := "E"
         cModo  := ""

         Escreve( cWord2, cWord1 )

      EndIf

      If nDecimal[nIndice] = 1

         nIndAux := ( 10 + nDecimal[nIndice + 1] )
         cWord2  := nUnidade[nIndAux]
         cWord1  := StrTran( cWord2 , "-" )
      Else

         cWord2 := nDezena[nDecimal[nIndice]]
         cWord1 := StrTran( cWord2 , "-" )

      EndIf

      Escreve( cWord2, cWord1 )

   EndIf

   If Str( nIndice , 2 ) $(" 3 6 912") .And. nDecimal[nIndice] > 0

      If nDecimal[nIndice -1] # 1

         If cModo = "P"

            cWord2 := "E"
            cWord1 := "E"
            cModo  := ""

            Escreve( cWord2, cWord1 )

         EndIf

         cWord2 := nUnidade[nDecimal[nIndice]]
         cWord1 := StrTran( cWord2 , "-" )

         Escreve( cWord2, cWord1 )

      EndIf

   EndIf

   If Str( nIndice , 2 ) $(" 3 6 912")

      If Str( nIndice , 2 ) == " 3" .And. ( nDecimal[nIndice] # 0 .Or. nDecimal[nIndice-1] # 0 .Or. nDecimal[nIndice-2] # 0)

         cVirgula := ""

         For a := 4 To 12

             If nDecimal[A] # 0

                cVirgula := ","

             EndIf

         Next a

         If nDecimal[nIndice - 2] # 0 .Or. nDecimal[nIndice - 1] # 0 .Or. nDecimal[nIndice] > 1

            cWord2 := "BI-LHOES" + cVirgula
            cWord1 := "BILHOES"  + cVirgula

         Else

            cWord2 := "BI-LHAO" + cVirgula
            cWord1 := "BILHAO"  + cVirgula

         EndIf

         Escreve( cWord2, cWord1 )

         cPrepos := "DE"

      ElseIf Str( nIndice , 2 ) == " 6" .And. (nDecimal[nIndice] # 0 .Or. nDecimal[nIndice-1] # 0 .Or. nDecimal[nIndice-2] # 0)

         cVirgula := ""

         For a := 7 To 12

             If nDecimal[A] # 0

                cVirgula := ","

             EndIf

         Next a

         If nDecimal[nIndice - 2] # 0 .Or. nDecimal[nIndice - 1] # 0 .Or. nDecimal[nIndice] > 1

            cWord2 := "MI-LHOES" + cVirgula
            cWord1 := "MILHOES"  + cVirgula

         Else

            cWord2 := "MI-LHAO" + cVirgula
            cWord1 := "MILHAO"  + cVirgula

         EndIf

         Escreve( cWord2, cWord1 )

         cPrepos := "DE"

      ElseIf Str( nIndice , 2 ) == " 9" .And. (nDecimal[nIndice] # 0 .Or. nDecimal[nIndice-1] # 0 .Or. nDecimal[nIndice-2] # 0)

         cVirgula := ""

         For a := 10 To 12

             If nDecimal[A] # 0

                cVirgula := ","

             EndIf

         Next a

         cWord2 := "MIL" + cVirgula
         cWord1 := "MIL" + cVirgula

         Escreve( cWord2, cWord1 )

         cPrepos := " "

      EndIf

   EndIf

   nIndice += 1

EndDo

If cPrepos # " "

   cWord2 := cPrepos
   cWord1 := cPrepos

   Escreve( cWord2, cWord1 )

EndIf

If Int( nValor ) == 1

   cWord2 := "RE-AL"
   cWord1 := "REAL"

Else

   cWord2 := "RE-A-IS"
   cWord1 := "REAIS"

EndIf

Escreve( cWord2, cWord1 )

If nDecimal[14] # 0 .Or. nDecimal[15] # 0

   cWord2 := "E"
   cWord1 := "E"

   Escreve( cWord2, cWord1 )

EndIf

If nDecimal[14] # 0

   If nDecimal[14] == 1

      nIndAux := 10 + nDecimal[15]
      cWord2  := nUnidade[nIndAux]
      cWord1  := StrTran( cWord2 , "-" )

   Else

      cWord2  := nDezena[nDecimal[14]]
      cWord1  := StrTran( cWord2 , "-" )

   EndIf

   Escreve( cWord2, cWord1 )

EndIf

If nDecimal[15] # 0

   If nDecimal[14] > 1

      cWord2 := "E"
      cWord1 := "E"

      Escreve( cWord2, cWord1 )

   EndIf

   If nDecimal[14] # 1

      cWord2 := nUnidade[nDecimal[15]]
      cWord1 := StrTran( cWord2 , "-" )

      Escreve( cWord2, cWord1 )

   EndIf

EndIf

If ( ( nDecimal[14] * 10 ) + nDecimal[15] ) # 0

   If ( ( nDecimal[14] * 10 ) + nDecimal[15] ) == 1

      cWord2 := "CEN-TA-VO"
      cWord1 := "CENTAVO"

   Else

      cWord2 := "CEN-TA-VOS"
      cWord1 := "CENTAVOS"

   EndIf

   Escreve( cWord2, cWord1 )

EndIf

For a := nNumLin To nQtdLin

   cVarExt[A] := cVarExt[A] + Replicate( cCharComp , nTamLin - Len( cVarExt[A] ) )

Next a
 
Static Function escreve( cWord2, cWord1 )
nTamWord := 0

If Len( cWord1 ) + 1 > nTamLin - nTamWord

   nPosicao := SubStr( cWord1 , 1 , nTamLin - nTamWord - 1 )
   nCountb  := 1

   For a := 1 To Len( nPosicao )

       If SubStr( cWord2 , A , 1 ) # SubStr( nPosicao , nCountb , 1 )

          nCountb += 1

          Loop

       EndIf

       nCountb += 1

   Next a

   For a := nCountb To 1 Step -1

       M_QUAESP = 0

       If SUBSTR(cWord2,A,1) = "-"

          For c := a To 1 Step -1

              If SubStr( cWord2 , C , 1 ) == "-"

                 M_QUAESP += 1

              EndIf

          Next c

          cVarExt[nNumLin] := cVarExt[nNumLin] + SubStr( cWord1 , 1 , A - M_QUAESP ) + "-"

          nTamWord += ( A - ( M_QUAESP + 1 ) )

          cModo = "P"

          Exit

       EndIf

   Next a

   cVarExt[nNumLin] := cVarExt[nNumLin] + Replicate( cCharComp ,nTamLin - nTamWord )

   nNumLin  := nNumLin + 1
   nTamWord := 0

   If a # 1

      cWord1 := Right( cWord1, Len( cWord1 ) - Len( SubStr( cWord1 , 1 , A - M_QUAESP ) ) )  + " "
      cVarExt[nNumLin] += cWord1

      nTamWord += Len( cWord1 )

   Else

      cVarExt[nNumLin] += ( cWord1 + " " )

      nTamWord += ( Len( cWord1 ) + 1 )

      cModo := "P"

   EndIf

Else

   cVarExt[nNumLin] = cVarExt[nNumLin] +( cWord1 + " " )

   nTamWord += ( Len( cWord1 ) + 1 )

   cModo := "P"

EndIf

Return

